//
//  LSLossPerCentCompanyVController.m
//  LookScore
//
//  Created by wr on 2017/12/20.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLossPerCentCompanyVController.h"
#import "LSLossPerCentCompanyCell.h"
#import "LSExponentModel.h"
#import "MBProgressHUD+MJ.h"
@interface LSLossPerCentCompanyVController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *modelArray;


@property (nonatomic, strong)NSMutableArray * selectCompanyArr;
@end

@implementation LSLossPerCentCompanyVController

- (NSMutableArray *)selectCompanyArr{
    if (!_selectCompanyArr) {
        _selectCompanyArr = @[].mutableCopy;
    }
    
    return _selectCompanyArr;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(NSMutableArray *)modelArray{
    if (!_modelArray) {
        _modelArray=[[NSMutableArray alloc]init];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"筛选公司";
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).bottomEqualToView(self.view);
    
    for (CompanyList * model in self.companyArr) {
        if (model.isSelected) {
            [self.selectCompanyArr addObject:model];
        }
    }
//    MyLog(@"%@",self.companyArr);
}



#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    return self.companyArr.count;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.row==self.modelArray.count) {
//
//    }
    if (indexPath.section==1) {
        return 31+LSButtonHeight;
    }
    return 35;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1) {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCellButton"];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellButton"];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.contentView.backgroundColor=LSBackGroundColor;
            UIButton *sureButton=[UIButton new];
            sureButton.frame=CGRectMake(LSSpaceLeftWidth2, 31, LSScreenWidth-LSSpaceLeftWidth2*2, LSButtonHeight);
            sureButton.titleLabel.font=LSFontSize15;
            sureButton.layer.cornerRadius=5;
            sureButton.layer.masksToBounds=YES;
            [sureButton setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
//            [sureButton setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor9dd3cc] forState:UIControlStateNormal];
//            [sureButton setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateHighlighted];
            [sureButton setBackgroundColor:LSButtonColor];
            sureButton.tag=10;
            [sureButton setTitle:@"确定" forState:UIControlStateNormal];
            [cell.contentView addSubview:sureButton];
            
        }
        UIButton *surebutton=[cell viewWithTag:10];
        [surebutton addTarget:self action:@selector(sureEvent:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
       LSLossPerCentCompanyCell *cell=[LSLossPerCentCompanyCell LossPerCentCompanyCellByTableView:tableView];
        cell.model = self.companyArr[indexPath.row];
        cell.indexPath = indexPath;
        
        KWeakSelf(self);
        cell.block = ^(UIButton *btn,NSIndexPath * myIndexPath) {
            
            if (btn.selected) {
                
                CompanyList * model = weakself.companyArr[myIndexPath.row];
                model.isSelected = YES;
                [weakself.selectCompanyArr addObject:model];
                
                [weakself.modelArray addObject:btn];
                
                //当已经选择3个，点击选择第4个时，则默认取消前3个中最先被选择的那项
                if (weakself.selectCompanyArr.count > 3) {
                    CompanyList * model = self.selectCompanyArr.firstObject;
                    [weakself.selectCompanyArr removeObject:model];
                    UIButton * unSelectBtn = (UIButton *)weakself.modelArray.firstObject;
//                    unSelectBtn.selected = NO;
                    model.isSelected = NO;
                    [weakself.modelArray removeObject:unSelectBtn];
                    
                }
            }else{
                 CompanyList * model = weakself.companyArr[myIndexPath.row];
                model.isSelected = NO;
                [weakself.selectCompanyArr removeObject:model];
                [weakself.modelArray removeObject:btn];
                
            }
            [self.tableView reloadData];
        };
        
        
    return cell;
        
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1) {
        
    }else{
        LSLossPerCentCompanyCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        cell.selectedBu.selected=!cell.selectedBu.selected;
        
        if (cell.selectedBu.selected) {
            
            CompanyList * model = self.companyArr[indexPath.row];
            model.isSelected = YES;
            [self.selectCompanyArr addObject:model];
            
            [self.modelArray addObject:cell.selectedBu];
            
            //当已经选择3个，点击选择第4个时，则默认取消前3个中最先被选择的那项
            if (self.selectCompanyArr.count > 3) {
                CompanyList * model = self.selectCompanyArr.firstObject;
                [self.selectCompanyArr removeObject:model];
                UIButton * unSelectBtn = (UIButton *)self.modelArray.firstObject;
                //                    unSelectBtn.selected = NO;
                model.isSelected = NO;
                [self.modelArray removeObject:unSelectBtn];
                
            }
        }else{
            CompanyList * model = self.companyArr[indexPath.row];
            model.isSelected = NO;
            [self.selectCompanyArr removeObject:model];
            [self.modelArray removeObject:cell.selectedBu];
            
        }
        [self.tableView reloadData];
        
    }
    
}


#pragma mark 🔥 Event

-(void)sureEvent:(UIButton *)sender{
    
    if (self.selectCompanyArr.count < 3) {
        [MBProgressHUD showOnlyMessage:@"至少选择3个公司" afterDelay:1.5f];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
    self.block(self.selectCompanyArr);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
